/**\file PolicyDiscrete.cpp
 *
 * Authors:
 * Frans Oliehoek <faolieho@science.uva.nl>
 * Matthijs Spaan <mtjspaan@isr.ist.utl.pt>
 *
 * Copyright 2008 Universiteit van Amsterdam, Instituto Superior Tecnico
 *
 * This file is part of MultiAgentDecisionProcess.
 *
 * MultiAgentDecisionProcess is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * MultiAgentDecisionProcess is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MultiAgentDecisionProcess.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * $Id: PolicyDiscrete.cpp 3604 2009-08-25 10:52:24Z mtjspaan $
 */

#include "PolicyDiscrete.h"
#include <stdlib.h>


Index PolicyDiscrete::SampleAction( Index i ) const
{
    size_t nrA = GetInterfacePTPDiscrete()->GetNrActions(_m_agentI);
    double randNr= ((double)rand()) / (RAND_MAX + 1.0);
    double sum=0;
    Index selected_a=0;

    for(Index a=0; a<nrA; a++)
    {
        sum+=GetActionProb(i, a);
        if(randNr<=sum)
        {
            selected_a=a;
            break;
        }
    }
    return(selected_a);
}
